package com.hwhhhh.mygrad.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.hwhhhh.mygrad.ClsInfoActivity;
import com.hwhhhh.mygrad.HomeActivity;
import com.hwhhhh.mygrad.R;
import com.hwhhhh.mygrad.dto.FreeClsDTO;
import com.hwhhhh.mygrad.view.IconfontView;

import java.util.List;

/**
 * 执行顺序：getItemCount getItemViewType onCreateViewHolder onBindViewHolder
 */
public class StickHeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    public static final int ITEM_TYPE_HEADER = 0;
    public static final int ITEM_TYPE_CONTENT = 1;
    private static final String TAG = "StickHeaderAdapter";
    private List<FreeClsDTO> data;

    public void setData(List<FreeClsDTO> data) {
        this.data = data;
    }

    public List<FreeClsDTO> getData() {
        return data;
    }

    private Context mContext;
    private String[] titles;


    public StickHeaderAdapter(Context context, List<FreeClsDTO> data, String[] titles) {
        this.mContext = context;
        this.data = data;
        this.titles = titles;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_HEADER) return new HeaderViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cls_title, parent, false));
        else if (viewType == ITEM_TYPE_CONTENT) return new ContentHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cls_list, parent, false));
        else return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder) {
            FreeClsDTO entity = data.get(position + 1);
            ((HeaderViewHolder) holder).tvClsTitle.setText(titles[entity.getSchoolBuilding() - 1]);
        } else if (holder instanceof ContentHolder){
            FreeClsDTO entity = data.get(position);
            ((ContentHolder) holder).tvClsNum.setText(entity.getClassroomNum());
            float prop = (float)entity.getOccupyNum() / entity.getCapacity();
            if (prop <= 0.201) {
                ((ContentHolder) holder).icState.setTextColor(mContext.getColor(R.color.success));
                Log.d(TAG, "onBindViewHolder: <=0.2:" + entity.getClassroomNum() +" prop:" + prop);
            } else if (prop <= 0.301){
                ((ContentHolder) holder).icState.setTextColor(mContext.getColor(R.color.busy));
                Log.d(TAG, "onBindViewHolder: <=0.3:" + entity.getClassroomNum() +" prop:" + prop);
            } else {
                ((ContentHolder) holder).icState.setTextColor(mContext.getColor(R.color.danger));
                Log.d(TAG, "onBindViewHolder: >0.3:" + entity.getClassroomNum() +" prop:" + prop);
            }
            String str = entity.getOccupyNum() + "/" + entity.getCapacity();
            ((ContentHolder) holder).tvClsCapacity.setText(str);
            ((ContentHolder) holder).tvClsType.setText(getClsType(entity.getClassroomType()));
            Log.d(TAG, "onBindViewHolder: id:" + entity.getId());
            ((ContentHolder) holder).rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ClsInfoActivity.class);
                    HomeActivity activity = (HomeActivity) mContext;
                    intent.putExtra("date", activity.curYear + "-" + activity.curMonth + "-" + activity.curDay);
//                    intent.putExtra("cls", entity);
                    intent.putExtra("freeClsId", entity.getId());
                    intent.putExtra("clsId", entity.getClassroomId());
                    mContext.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        int sb = data.get(position).getSchoolBuilding();
        if (sb == -1) return ITEM_TYPE_HEADER;
        return ITEM_TYPE_CONTENT;
    }

    public String getClsType(int type) {
        if (type == 0) return "普通教室";
        else if (type == 1) return "多媒体教室";
        else if (type == 2) return "机房";
        else if (type == 3) return "阶梯教室";
        return "普通教室1";
    }

    public static class HeaderViewHolder extends RecyclerView.ViewHolder{
        TextView tvClsTitle;

        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
            tvClsTitle = itemView.findViewById(R.id.tv_cls_title);
        }
    }

    public static class ContentHolder extends RecyclerView.ViewHolder{
        TextView tvClsNum, tvClsCapacity, tvClsType;
        View rootView;
        IconfontView icState;

        public ContentHolder(@NonNull View itemView) {
            super(itemView);
            rootView = itemView;
            tvClsNum = itemView.findViewById(R.id.tv_cls_num);
            tvClsCapacity = itemView.findViewById(R.id.tv_cls_capacity);
            tvClsType = itemView.findViewById(R.id.tv_cls_type);
            icState = itemView.findViewById(R.id.ic_state);
        }
    }
}
